import 'package:flutter/material.dart';
import 'package:framelib/utils/color_util.dart';
import 'package:framelib/utils/px_util.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class FloatingDragActionButtonLocation extends FloatingActionButtonLocation {
  double screenWidth;
  double screenHeight;
  double statusBarHeight;
  double offsetX;    // X方向的偏移量
  double offsetY;    // Y方向的偏移量
  FloatingDragActionButtonLocation(this.screenWidth,this.screenHeight,this.statusBarHeight,this.offsetX, this.offsetY);

  @override
  Offset getOffset(ScaffoldPrelayoutGeometry scaffoldGeometry) {
    double floatingActionButtonSizeWidth = scaffoldGeometry.floatingActionButtonSize?.width??0;
    if(offsetX<0)offsetX=0;
    if(offsetX>(screenWidth-floatingActionButtonSizeWidth))offsetX=(screenWidth-floatingActionButtonSizeWidth);

    double floatingActionButtonSizeHeight = scaffoldGeometry.floatingActionButtonSize?.height??0;
    if(offsetY<statusBarHeight)offsetY=statusBarHeight;
    if(offsetY>(screenHeight-floatingActionButtonSizeHeight-kBottomNavigationBarHeight))offsetY=(screenHeight-floatingActionButtonSizeHeight-kBottomNavigationBarHeight);

    return Offset(offsetX, offsetY);
  }
}

class FloatingDragActionButton extends StatelessWidget {

  double _floatActionBtnHeight = px_56.sp;

  @override
  Widget build(BuildContext context) {
    double screenWidth = MediaQuery.sizeOf(context).width;
    double screenHeight = MediaQuery.sizeOf(context).height;
    _floatActionBtnHeight = screenWidth<screenHeight ? px_66.sp : px_21.sp;

    // TODO: implement build
    return Card(
      elevation: px_6.sp,
      color: Colors.transparent,
        shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.all(
          Radius.circular(_floatActionBtnHeight / 2),
        ),
      ),
      child: Container(
        height: _floatActionBtnHeight,
        width: _floatActionBtnHeight,
        decoration: BoxDecoration(
          color: color_main,
          borderRadius: BorderRadius.all(
            Radius.circular(_floatActionBtnHeight / 2),
          ),
        ),
        alignment: Alignment.center,
        child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              Container(
                child: Icon(
                  Icons.edit,
                  size: screenWidth<screenHeight?px_23.sp:px_7.sp,
                  color: color_white,
                ),
              ),
              Container(
                margin: EdgeInsets.only(
                  bottom: px_5.h
                ),
                child: Text(
                  '拖动',
                  style: TextStyle(
                      fontSize: screenWidth<screenHeight?px_14.sp:px_5.sp,
                      color: color_white,
                      fontWeight: FontWeight.w500
                  ),
                ),
              ),
            ],
        ),
      ),
    );
  }

}